import openpyxl
import pandas as pd
import xlrd
from data_processing import Data
from links import links
from database import database
import mark


filename = r'副本SP功能降级_仿真_V1.2_20221219.xls'
file = r'./5_4/-con2-con3-con4.txt'
# in_file = r'./1_2/-con9-con10.txt'
out_file = r'./5_4/out54.txt'
mark_file = r'./5_4/-con2-con3-con4+con1.txt'
f = open(file, 'r', encoding='UTF-8')
out_f = open(out_file, 'w')
mark_f = open(mark_file, 'w')
SP = xlrd.open_workbook(filename)
# sheetnames = SP.sheet_names()
# print(sheetnames)
main_sheet = SP.sheet_by_index(1)                    # 获取主工作表格
print(main_sheet)
x1 = 454                                            # 读取行数为x1+1,例要读取15行开始，请输入14
x2 = 553                                              # 结尾行不被读取，例要读取至17行，请输入17
judgment = main_sheet.col_values(6, x1-1, x2)          # 读取x1-x2行内容
i = x1+1
marky = mark.Mark()
for judge in judgment:
    if judge == '':
        i += 1
        continue
    print(i)
    # data = Data(judge, i)
    data = links(judge, i)
    data.T()
    data.readout()
    out = ''
    if data.symbol == '&':
        out = '&&'
    elif data.symbol == '||':
        out = '||'
    out = out + data.out_out
    if data.excep == 0:
        out_f.write(out)
        out_f.flush()
        # 上方为对out输出，下方为对mark端口的输出
        if data.t == 1:
            if marky[data.out][0] == '0':
                marky[data.out][0] = data.lock
                marky[data.out][1] = data.lock_val
            else:
                if int(marky[data.out][1]) == int(data.lock_val):
                    pass
                else:
                    print(f"{i}行mark{data.out}存在录入数据错误，其值已锁定")
            # mark_f.write(f"mark({data.out},:)\t=[{data.lock},{data.lock_val}];%{data.mark}\n")
        elif data.t == 2:
            if marky[data.out][0] == '0':
                marky[data.out][0] = data.lock
                marky[data.out][1] = data.lock_val
            else:
                if int(marky[data.out][1]) == int(data.lock_val):
                    pass
                else:
                    print(f"{i}行mark{data.out}存在录入数据错误，其值已锁定")
            lost = str(int(data.out) + 1)
            if marky[lost][0] == '0':
                marky[lost][0] = data.two_lock
                marky[lost][1] = data.two_lock_val
            else:
                if int(marky[lost][1]) == int(data.two_lock_val):
                    pass
                else:
                    print(f"{i}行mark{lost}存在录入数据错误，其值已锁定")
            # mark_f.write(f"mark({data.out},:)\t=[{data.lock},{data.lock_val}];%{data.mark}1\n")
            # mark_f.write(f"mark({int(data.out) + 1},:)\t=[{data.two_lock},{data.two_lock_val}];%{data.mark}2\n")
        elif data.t == 3:
            if marky[data.out][0] == '0':
                marky[data.out][0] = data.lock
                marky[data.out][1] = data.lock_val
            else:
                if int(marky[data.out][1]) == int(data.lock_val):
                    pass
                else:
                    print(f"{i}行mark{data.out}存在录入数据错误，其值已锁定")
            lost = str(int(data.out) + 1)
            if marky[lost][0] == '0':
                marky[lost][0] = data.two_lock
                marky[lost][1] = data.two_lock_val
            else:
                if int(marky[lost][1]) == int(data.two_lock_val):
                    pass
                else:
                    print(f"{i}行mark{lost}存在录入数据错误，其值已锁定")
            lost1 = str(int(data.out) + 2)
            if marky[lost1][0] == '0':
                marky[lost1][0] = data.three_lock
                marky[lost1][1] = data.three_lock_val
            else:
                if int(marky[lost1][1]) == int(data.three_lock_val):
                    pass
                else:
                    print(f"{i}行mark{lost1}存在录入数据错误，其值已锁定")
            # mark_f.write(f"mark({data.out},:)\t=[{data.lock},{data.lock_val}];%{data.mark}1\n")
            # mark_f.write(f"mark({int(data.out) + 1},:)\t=[{data.two_lock},{data.two_lock_val}];%{data.mark}2\n")
            # mark_f.write(f"mark({int(data.out) + 2},:)\t=[{data.three_lock},{data.three_lock_val}];%{data.mark}3\n")
    else:
        print(f"{i}行写入失败")
    data.printf()
    i += 1
out_f.close()
# 对Mark端口进行输出
i0 = 1
while i0 < 208:
    s = f'mark({i0},:)\t=[{marky[str(i0)][0]},{marky[str(i0)][1]}];% {marky[str(i0)][2]}\n'
    mark_f.write(s)
    mark_f.flush()
    i0 += 1
mark_f.close()










